<?php

namespace App\Http\Controllers;

use App\Models\Goods;
use App\Models\GoodsCat;
use App\Models\GoodsStock;
use App\Models\GoodsStockHis;

use App\Models\Users;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class GoodsStockController extends BaseController
{

    /**
     * 商品库存操作记录列表
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $goods_name = $request->get("goodsName");
        $user_name = $request->get("userName");
        $goods_action = $request->get("goodsAction");
        $start_date = $request->get("start_date");
        $end_date = $request->get("end_date");
        $pageNum = $request->get("page_num");
        $pageSize = $request->get("page_size", 10);

        $where = [['status',1]];
        if ($goods_action) {
            $where[] = ["goods_action", $goods_action];
        }

        if ($start_date) {
            $where[] = ["date", ">=", $start_date . " 00:00:00"];
        }

        if ($end_date) {
            $where[] = ["date", "<=", $end_date . " 23:59:59"];
        }
        $goods_id = [];
        if ($goods_name) {
            $goodsInfo = Goods::where([["name", "like", "%" . $goods_name . "%"], ['status', 1]])->select("id", "name")->get();
            if (!$goodsInfo || empty($goodsInfo->toArray())) {
                return Success(10100, [
                    "data" => [], "page_info" => [
                        'current' => $pageNum, 'page_size' => (int)$pageSize, "count" => 0,
                        "pages" => 0
                    ]
                ]);
            }
            $goods_id = array_column($goodsInfo->toArray(), "id");
        }
        $user_id = [];
        if ($user_name) {
            $userInfo = Users::where([["nickname", "like", "%" . $user_name . "%"], ['status', 1]])->select("id", "name")->get();
            if (!$userInfo || empty($userInfo->toArray())) {
                return Success(10100, [
                    "data" => [], "page_info" => [
                        'current' => $pageNum, 'page_size' => (int)$pageSize, "count" => 0,
                        "pages" => 0
                    ]
                ]);
            }
            $user_id = array_column($userInfo->toArray(), "id");
        }

        $stockList = GoodsStockHis::where($where)
            ->with(['userInfo', 'goodsInfo'])
            ->where(function ($query) use ($goods_id) {
                return $query->when($goods_id, function ($query) use ($goods_id) {
                    return $query->whereIn('goods_id', $goods_id);
                });

            })
            ->where(function ($query) use ($user_id) {
                return $query->when($user_id, function ($query) use ($user_id) {
                    return $query->whereIn('user_id', $user_id);
                });

            })
            ->orderBy("date", "desc")
            ->paginate($pageSize, ['*'], "page", $pageNum);
        $list = $stockList->getCollection();
        return Success(10100, [
            "data" => $list,
            "page_info" => [
                'current' => $stockList->currentPage(), 'page_size' => (int)$pageSize, "count" => $stockList->total(),
                "pages" => $stockList->lastPage()
            ]
        ]);

    }
}
